﻿using System;
using System.Web.UI.WebControls;
using ContractManagementSystem.BusinessLogic;
using ContractManagementSystem.Entity;

namespace ContractManagementSystem
{
    public partial class ViewAllContract : System.Web.UI.Page
    {
        private static Guid _id;
        static readonly PagedDataSource PagedDataSource = new PagedDataSource();
        static int _pageNumber;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ViewPanel.Visible = true;
                EditPanel.Visible = false;
                FetchAll();
            }
        }

        protected void ListSorting(object sender, ListViewSortEventArgs e)
        {
            string sortColumn = e.SortExpression;
            SortDirection sortDirection = (SortDirection)e.SortDirection;
            ContractListView.Sort(sortColumn, sortDirection);
            ContractListView.DataBind();
        }

        private void FetchAll()
        {
            if (Session["Name"] != null)
            {
                var sessionUser = Session["Name"].ToString();
                if (sessionUser.Equals("Admin"))
                {
                    PagedDataSource.DataSource = ContractBusiness.FetchAll().DefaultView;
                    DataPaging();
                }
            }
            else
            {
                Response.Redirect("LogOn.aspx");
            }

            var list = ContractTypeBusiness.FetchAll();
            ContractTypeViewDropDownList.DataSource = list;
            ContractTypeViewDropDownList.DataTextField = "Name";
            ContractTypeViewDropDownList.DataValueField = "ContractTypeId";
            ContractTypeViewDropDownList.DataBind();
            ContractTypeViewDropDownList.Items.Insert(0, "All");
        }

        private void DataPaging()
        {
            PagedDataSource.PageSize = 15;
            PagedDataSource.CurrentPageIndex = _pageNumber;
            PagedDataSource.AllowPaging = true;

            if (PagedDataSource.IsFirstPage)
            {
                FirstPageButton.Visible = false;
                PreviewPageButton.Visible = false;
                NextPageButton.Visible = true;
                LastPageButton.Visible = true;
            }

            if (PagedDataSource.IsLastPage)
            {
                LastPageButton.Visible = false;
                NextPageButton.Visible = false;
                PreviewPageButton.Visible = true;
                FirstPageButton.Visible = true;
            }

            PageLabel.Text = (_pageNumber + 1) + "/" + PagedDataSource.PageCount;
            ContractListView.DataSource = PagedDataSource;
            ContractListView.DataBind();
        }

        private void AddValueControl()
        {
            ContractorDropDownList.Items.Add("- Choose Contractor -");
            //var contractorBusiness = new ContractorBussiness();
            ContractorDropDownList.DataSource = ContractorBusiness.FetchAll(1);
            ContractorDropDownList.DataTextField = "FullName";
            ContractorDropDownList.DataValueField = "ContractorId";
            ContractorDropDownList.DataBind();

            ContractTypeDropDownList.Items.Add("- Choose ContractType -");
            //var contractTypeBusiness = new ContractTypeBusiness();
            ContractTypeDropDownList.DataSource = ContractTypeBusiness.FetchAll();
            ContractTypeDropDownList.DataTextField = "Name";
            ContractTypeDropDownList.DataValueField = "ContractTypeId";
            ContractTypeDropDownList.DataBind();

            ManagerDropDownList.Items.Add("- Choose Manager -");
            //var lineManagerBusiness = new LineManagerBussiness();
            ManagerDropDownList.DataSource = LineManagerBusiness.FetchAll(1);
            ManagerDropDownList.DataTextField = "FullName";
            ManagerDropDownList.DataValueField = "ManagerId";
            ManagerDropDownList.DataBind();

            ConsultantDropDownList.Items.Add("- Choose Consultant -");
            //var consultantBusiness = new ConsultantBussiness();
            ConsultantDropDownList.DataSource = ConsultantBusiness.FetchAll(1);
            ConsultantDropDownList.DataTextField = "FullName";
            ConsultantDropDownList.DataValueField = "ConsultantId";
            ConsultantDropDownList.DataBind();

        }

        protected void DeleteOnCommand(object sender, CommandEventArgs commandEventArgs)
        {
            ContractBusiness.Delete(commandEventArgs.CommandArgument.ToString());
            FetchAll();
        }

        protected void EditOnCommand(object sender, CommandEventArgs commandEventArgs)
        {
            ViewPanel.Visible = false;
            EditPanel.Visible = true;
            AddValueControl();

            var entityFocus = ContractBusiness.FetchByContractId(commandEventArgs.CommandArgument.ToString());
            ContractorDropDownList.Text = entityFocus.ContractorId.ToString();
            ContractTypeDropDownList.Text = entityFocus.ContractTypeId.ToString();
            ManagerDropDownList.Text = entityFocus.ManagerId.ToString();
            ConsultantDropDownList.Text = entityFocus.ConsultantId.ToString();
            StartDateTextBox.Text = entityFocus.StartDate.ToShortDateString();
            EndDateTextBox.Text = entityFocus.EndDate.ToShortDateString();
            ContentTextBox.Text = entityFocus.Content;
            StartDateTextBox.Enabled = false;
            EndDateTextBox.Enabled = false;
            _id = new Guid(commandEventArgs.CommandArgument.ToString());
        }

        protected void UpdateButtonClick(object sender, EventArgs e)
        {
            var entityFocus = new ContractEntity
            {
                ContractId = _id,
                ContractorId = new Guid(ContractorDropDownList.SelectedValue),
                ContractTypeId = new Guid(ContractTypeDropDownList.SelectedValue),
                ManagerId = new Guid(ManagerDropDownList.SelectedValue),
                ConsultantId = new Guid(ConsultantDropDownList.SelectedValue),
                Content = ContentTextBox.Text
            };

            if (ContractBusiness.Update(entityFocus))
            {
                ViewPanel.Visible = true;
                EditPanel.Visible = false;
            }
        }

        protected void BackButtonClick(object sender, EventArgs e)
        {
            ViewPanel.Visible = true;
            EditPanel.Visible = false;
        }

        private void Search()
        {
            if (ContractTypeViewDropDownList.Text == "All")
            {
                if (String.IsNullOrEmpty(StartTextBox.Text) && String.IsNullOrEmpty(EndTextBox.Text))
                {
                    PagedDataSource.DataSource = ContractBusiness.FetchAllWithContractor(ContractorNameTextBox.Text.Trim(), "", "", "").DefaultView;
                }
                else
                {
                    PagedDataSource.DataSource = ContractBusiness.FetchAllWithContractor(ContractorNameTextBox.Text.Trim(), "", StartTextBox.Text, EndTextBox.Text).DefaultView;
                }

            }
            else
            {
                if (String.IsNullOrEmpty(StartDateTextBox.Text) && String.IsNullOrEmpty(EndDateTextBox.Text))
                {
                    PagedDataSource.DataSource = ContractBusiness.FetchAllWithContractor(ContractorNameTextBox.Text.Trim(), ContractTypeViewDropDownList.SelectedValue, "", "").DefaultView;
                }
                else
                {
                    PagedDataSource.DataSource = ContractBusiness.FetchAllWithContractor(ContractorNameTextBox.Text.Trim(), ContractTypeViewDropDownList.SelectedValue, StartTextBox.Text, EndTextBox.Text).DefaultView;
                }

            }

            DataPaging();
        }

        private void FetchAllByContractType(string typeId)
        {
            PagedDataSource.DataSource = ContractBusiness.FetchAllWithContractType(new Guid(typeId)).DefaultView;
            DataPaging();
        }

        protected void SearchButton_OnClick(object sender, EventArgs e)
        {
            Search();
        }

        protected void ContractTypeViewDropDownList_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            if (ContractTypeViewDropDownList.Text == "All")
            {
                FetchAll();
            }
            else
            {
                FetchAllByContractType(ContractTypeViewDropDownList.SelectedValue);
            }
        }

        protected void LastPageButtonClick(object sender, EventArgs e)
        {
            _pageNumber = PagedDataSource.PageCount - 1;
            FetchAll();
            LastPageButton.Visible = false;
        }

        protected void FirstPageButtonClick(object sender, EventArgs e)
        {
            _pageNumber = 0;
            FetchAll();
            FirstPageButton.Visible = false;
        }

        protected void PreviewPageButtonClick(object sender, EventArgs e)
        {
            if (!PagedDataSource.IsFirstPage)
            {
                _pageNumber--;
                FetchAll();
                NextPageButton.Visible = true;
                LastPageButton.Visible = true;
            }
        }

        protected void NextPageButtonClick(object sender, EventArgs e)
        {
            if (!PagedDataSource.IsLastPage)
            {
                _pageNumber++;
                FetchAll();
                PreviewPageButton.Visible = true;
                FirstPageButton.Visible = true;
            }
        }

        protected void ClearButton_OnClick(object sender, EventArgs e)
        {
            FetchAll();
            ContractTypeViewDropDownList.SelectedIndex = -1;
            ContractorNameTextBox.Text = String.Empty;
            StartTextBox.Text = String.Empty;
            EndTextBox.Text = String.Empty;
        }
    }
}
